<template>
  <div class="page-wrap">
    <el-card shadow="never">
      <div class="toolbar">
        <el-input v-model="filters.search" placeholder="搜索收银点/终端SN" clearable class="w-300" />
        <el-button type="primary" @click="openEdit()">新增收银点</el-button>
        <el-button @click="exportCsv">导出</el-button>
      </div>
      <el-table :data="dataList" border stripe>
        <el-table-column prop="name" label="名称" min-width="160" />
        <el-table-column prop="sn" label="终端SN" width="160" />
        <el-table-column prop="channel" label="渠道" width="120" />
        <el-table-column prop="status" label="状态" width="100" />
        <el-table-column label="所属门店" min-width="160">
          <template #default="{ row }">{{ storeMap[row.storeId] || row.storeId }}</template>
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right">
          <template #default="{ row }">
            <el-button size="small" @click="openEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="onDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="visible" :title="form.id?'编辑收银点':'新增收银点'" width="520px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="名称"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="终端SN"><el-input v-model="form.sn" /></el-form-item>
        <el-form-item label="渠道"><el-select v-model="form.channel"><el-option label="线下" value="线下"/><el-option label="小程序" value="小程序"/><el-option label="三方平台" value="三方平台"/></el-select></el-form-item>
        <el-form-item label="状态"><el-select v-model="form.status"><el-option label="启用" value="enabled"/><el-option label="停用" value="disabled"/></el-select></el-form-item>
        <el-form-item label="所属门店">
          <el-select v-model="form.storeId" filterable>
            <el-option v-for="s in storeList" :key="s.id" :label="s.name" :value="s.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="visible=false">取消</el-button>
        <el-button type="primary" @click="onSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, onMounted, reactive, ref } from 'vue'
import { useStoreStore } from '@/stores/store.js'
const store = useStoreStore()
const filters = reactive({ search: '' })
const visible = ref(false)
const form = reactive({ id:'', name:'', sn:'', channel:'线下', status:'enabled', storeId:'' })

onMounted(()=>{ store.initialize() })

const storeList = computed(()=> store.stores)
const storeMap = computed(()=> Object.fromEntries(store.stores.map(s=>[s.id, s.name])))
const dataList = computed(()=>{
  const s = (filters.search||'').toLowerCase()
  return store.registers.filter(x=> !s || x.name.toLowerCase().includes(s) || (x.sn||'').toLowerCase().includes(s))
})

function openEdit(row){ Object.assign(form, row || { id:'', name:'', sn:'', channel:'线下', status:'enabled', storeId: storeList.value[0]?.id || '' }); visible.value=true }
function onSave(){ if(form.id) store.updateRegister(form.id, form); else store.addRegister(form); visible.value=false }
function onDelete(row){ store.removeRegister(row.id) }
function exportCsv(){
  const headers = ['名称','SN','渠道','状态','门店']
  const rows = store.registers.map(r=>[r.name,r.sn,r.channel,r.status,storeMap.value[r.storeId]||r.storeId])
  const csv = [headers.join(','), ...rows.map(r=>r.join(','))].join('\n')
  const blob = new Blob(['\ufeff' + csv], { type: 'text/csv;charset=utf-8;' })
  const link = document.createElement('a')
  link.href = URL.createObjectURL(blob)
  link.download = `收银点_${new Date().toISOString().slice(0,10)}.csv`
  link.click()
}
</script>

<style scoped>
.page-wrap { padding: 16px; }
.toolbar { display: flex; gap: 12px; margin-bottom: 12px; }
.w-300 { width: 300px; }
</style>


